<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>禅林瑜伽管理端</title>
  <!-- 引入样式 -->
  <link rel="stylesheet" href="../../plugins/element-ui/index.css" />
  <link rel="stylesheet" href="../../styles/common.css" />
  <link rel="stylesheet" href="../../styles/page.css" />
  <style>
    #member-app  .notAdmin::after{
      border: 0 !important;

    }
  </style>
</head>
<body>
  <div class="dashboard-container" id="member-app">
    <div class="container">
      <div class="tableBar">
        <el-input
          v-model="input"
          placeholder="请输入会员姓名"
          style="width: 250px"
          clearable
           @keyup.enter.native="handleQuery"
        >
          <i
            slot="prefix"
            class="el-input__icon el-icon-search"
            style="cursor: pointer"
            @click="handleQuery"
          ></i>
        </el-input>

          <el-button @click="guoqiHandle()" type="info">已过期会员查询</el-button>
        <el-button
          type="primary"
          @click="addMemberHandle('add')"
        >
          + 添加会员
        </el-button>
      </div>
      <el-table
        :data="tableData"
        stripe
        class="tableBox"
      >
        <el-table-column
          prop="name"
          label="会员姓名"
        ></el-table-column>

        <el-table-column
          label="时间卡会员是否过期"
        >
            <template slot-scope="scope">
              <el-tag v-if="scope.row.guoqi == '1'" type="success">未过期</el-tag>
              <el-tag v-if="scope.row.guoqi == '0'" type="danger">已过期</el-tag>
            </template>
        </el-table-column>

        <el-table-column
        prop="startTime"
        label="会员开始"
      ></el-table-column>
      
        <el-table-column
          prop="stopTime"
          label="会员到期"
        ></el-table-column>
        
        <el-table-column
          prop="phone"
          label="手机号"
        ></el-table-column>

        <el-table-column label="次卡状态">
          <template slot-scope="scope">
              <div v-if="new Date(scope.row.cardStopdate) >= new Date()">
            {{ String(scope.row.countCard) === '0' ? '无次卡数' : scope.row.countCard }}
              </div>
              <div style="color: red;" v-if="new Date(scope.row.cardStopdate) < new Date()">
                  {{scope.row.countCard}}次-已过期
              </div>
          </template>

        </el-table-column>
        <el-table-column
          label="操作"
          width="160"
          align="center"
        >
          <template slot-scope="scope">

            <el-button
              v-if="scope.row.countCard>0"
              type="text"
              size="small"
              class="blueBug"
              @click="statusHandle(scope.row)"
              :class="{notAdmin:user !== 'admin'}"
            >
              次卡消费
            </el-button>



            <el-button
              type="text"
              size="small"
              class="blueBug"
              @click="addMemberHandle(scope.row.id)"
              :class="{notAdmin:user !== 'admin'}"
            >
              编辑
            </el-button>
            <el-button
                  type="text"
                  size="small"
                  class="blueBug"
                  @click="deleteHandle(scope.row.id)"
             >
              删除
            </el-button>
<!--            <el-button-->
<!--              type="text"-->
<!--              size="small"-->
<!--              class="delBut non"-->
<!--              @click="statusHandle(scope.row)"-->
<!--              v-if="user === 'admin'"-->
<!--            >-->
<!--              {{ scope.row.status == '1' ? '禁用' : '启用' }}-->
<!--            </el-button>-->
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        class="pageList"
        :page-sizes="[10,20,30]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="counts"
        :current-page.sync="page"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      ></el-pagination>
    </div>
  </div>
    <!-- 开发环境版本，包含了有帮助的命令行警告 -->
    <script src="../../plugins/vue/vue.js"></script>
    <!-- 引入组件库 -->
    <script src="../../plugins/element-ui/index.js"></script>
    <!-- 引入axios -->
    <script src="../../plugins/axios/axios.min.js"></script>
    <script src="../../js/request.js"></script>
    <script src="../../api/member.js"></script>
    <script>
      new Vue({
        el: '#member-app',
        data() {
          return {
             input: '',
             counts: 0,
             page: 1,
             pageSize: 10,
             tableData : [],
             id : '',
             status : '',
             countCard: 0
          }
        },
        computed: {},

        /*vue初始化挂载函数*/
        created() {

          this.init();

          // alert(1439057691107037185);//js失去精度测试
          if(localStorage.getItem('userInfo')!=null){
            //获取当前登录会员的账号，并赋值给模型数据
            this.user = JSON.parse(localStorage.getItem('userInfo')).username;
          }
        },
        mounted() {
        },
        methods: {
          async init () {
            const params = {
              page: this.page,
              pageSize: this.pageSize,
              name: this.input ? this.input : undefined
            };
            await getMemberList(params).then(res => {
              if (String(res.code) === '1') {
                this.tableData = res.data.records || [];
                // 到期状态判断处理
                this.daoqizhuangtai();
                console.log(this.tableData);

                this.counts = res.data.total
              }
            }).catch(err => {
              this.$message.error('请求出错了：' + err)
            })
          },
          daoqizhuangtai(){

            for(let i = 0;i<this.tableData.length;i++){
              let stopTime = new Date(this.tableData[i].stopTime);
              let currentTime = new Date();
              if(stopTime > currentTime){
                this.tableData[i].guoqi='1';
              }else{
                this.tableData[i].guoqi='0';
              }
            }
          },guoqiHandle(){
            let tdata  = this.tableData;
            this.tableData = [];
            for(let i = 0;i<tdata.length;i++){
                let temp = tdata[i];

                let guoqi = temp.guoqi;
                if(guoqi == '0'){
                    this.tableData.push(temp);
                }else if(new Date(temp.cardStopdate) > new Date()){
                    this.tableData.push(temp);
                }

            }
            console("过期会员："+this.tableData);
          },
          handleQuery() {
            this.page = 1;
            this.init();
          },
           // 添加&修改跳转
          addMemberHandle (st) {
            if (st === 'add'){
              window.parent.menuHandle({
                id: '2',
                url: '/backend/page/member/add.html',
                name: '添加会员'
              },true)
            } else {
              window.parent.menuHandle({
                id: '2',
                url: '/backend/page/member/add.html?id='+st,//跳转到add.html页面并携带一个id参数
                name: '修改会员'
              },true)
            }
          },

          //次卡修改
          statusHandle (row) {
            this.id = row.id;
            this.countCard = row.countCard;
            this.$confirm('确认该用户消费次卡一次?'+row.countCard+'次->'+(row.countCard-1)+'次', '提示', {
              'confirmButtonText': '确定',
              'cancelButtonText': '取消',
              'type': 'warning'
              }).then(() => {//点击确定执行
                // 调用请求方法
                let count = row.countCard-1;

                countCardjian({'id': this.id, 'countCard':count}).then(res => {//请求结果处理
                console.log('countCardjian',res);
                if (String(res.code) === '1') {
                  this.$message.success(row.name+'次卡'+row.countCard+'次->'+(row.countCard-1)+'次;'+'更改成功！');
                  this.handleQuery()
                }
              }).catch(err => {
                this.$message.error('请求出错了：' + err)
              })
            })
          },
          handleSizeChange (val) {
            this.pageSize = val;
            this.init()
          },
          handleCurrentChange (val) {
            this.page = val;
            this.init()
          },
            // 删除
          deleteHandle (id) {
            this.$confirm('确认删除该会员, 是否继续?', '确定删除', {
               'confirmButtonText': '确定',
               'cancelButtonText': '取消',
                }).then(() => {
                 deleteMember(id).then(res => {
                    if (res.code === 1) {
                      this.$message.success('删除成功！');
                        this.handleQuery()
                    } else {
                      this.$message.error(res.msg || '操作失败')
                    }
                 }).catch(err => {
                    this.$message.error('请求出错了：' + err)
                 })
            })
           }
        }
      })
    </script>
</body>
</html>